package com.atguigu.self.gulimall.gateway.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.reactive.CorsWebFilter;
import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource;

/**
 * 在网关中所有服务统一配置CORS跨域
 *
 * @Author Li Xinyang  <lixinyang@xxx.com>  2021/1/20 19:09
 **/
@Configuration
public class GulimallCorsConfiguration {

    @Bean
    public CorsWebFilter corsWebFilter() {
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        CorsConfiguration configuration = new CorsConfiguration();
        // 允许所有请求头
        configuration.addAllowedHeader("*");
        // 允许所有请求方式
        configuration.addAllowedMethod("*");
        // 允许所有目标源可访问
        configuration.addAllowedOrigin("*");
        // 允许 携带cooker数据
        configuration.setAllowCredentials(true);
        source.registerCorsConfiguration("/**", configuration);
        return new CorsWebFilter(source);
    }
}
